//
// Created by wenyaxi on 24-10-29.
//
#include <common.h>
#include <deque>

class Solution {
public:
    deque<TreeNode *> deq;

    vector<vector<int> > levelOrder(TreeNode *root) {
        vector<vector<int> > res;
        if (root == nullptr) {
            return res;
        }
        deq.push_back(root);
        int c = 1;
        while (!deq.empty()) {
            int c2 = 0;
            vector<int> v;
            for (int i = 0; i < c; i++) {
                auto tree_node = deq.front();
                v.push_back(tree_node->val);
                deq.pop_front();
                if (tree_node->left) {
                    deq.push_back(tree_node->left);
                    c2++;
                }
                if (tree_node->right) {
                    deq.push_back(tree_node->right);
                    c2++;
                }
            }
            res.push_back(v);
            c = c2;
        }
        return res;
    }
};

int main(int argc, char *argv[]) {
    Solution s;
    TreeNode *p = new TreeNode(1,new TreeNode(2),new TreeNode(3));
    s.levelOrder(p);
}
